
# clear environment
rm(list=ls())

# make sure working directory is set to folder within which this script is saved
getwd()

# load data
lucid <- read.csv("Negativity+Bias_Summer+2019_v2_October+27,+2021_14.57.csv", stringsAsFactors = F)
lucid_info <- read.csv("DataAnalysis_6200459_Negativity_Bias_Fall_2019_2021-10-27_1531.csv", stringsAsFactors = F)

# remove test cases
tests <- tolower(lucid_info$Response.ID[lucid_info$PID == "test"])
lucid <- lucid[-which(lucid$rid %in% tests), ]
nrow(lucid) # 1,658

#### Data Quality Check ####

## Survey Time
start_time_code <- strsplit(as.character(lucid$StartDate), " ")
end_time_code <- strsplit(as.character(lucid$EndDate), " ")

start_date <- c()
end_date <- c()
start_hour_min <- c()
end_hour_min <- c()

for(i in 1:length(lucid$StartDate)) {
  start_date <- append(start_date, unlist(start_time_code[[i]])[1])
  end_date <- append(end_date, unlist(end_time_code[[i]])[1])
  start_hour_min <- append(start_hour_min, unlist(start_time_code[[i]])[2])
  end_hour_min <- append(end_hour_min, unlist(end_time_code[[i]])[2])
}

start_hour_min <- strsplit(start_hour_min, ":")
end_hour_min <- strsplit(end_hour_min, ":")

start_hour <- c()
start_minute <- c()
end_hour <- c()
end_minute <- c()

for(i in 1:length(lucid$StartDate)) {
  start_hour <- append(start_hour, unlist(start_hour_min[[i]])[1])
  start_minute <- append(start_minute, unlist(start_hour_min[[i]])[2])
  end_hour <- append(end_hour, unlist(end_hour_min[[i]])[1])
  end_minute <- append(end_minute, unlist(end_hour_min[[i]])[2])
}

start_hour <- as.numeric(start_hour) * 60
start_minute <- as.numeric(start_minute)
start_time <- start_hour + start_minute

end_hour <- as.numeric(end_hour) * 60
end_minute <- as.numeric(end_minute)
end_time <- end_hour + end_minute

same_day <- ifelse(start_date == end_date, 1, 0)
which(same_day == 0) # respondents did not complete over same day, so times are wrong

lucid$total_time <- ifelse(same_day == 1, end_time - start_time, NA)
summary(lucid$total_time)


## Validation Checks

# remove responses outside collection window (10/30-11/9, Oct 22nd pilot tested survey)
names(table(start_date))
good_dates <- names(table(start_date))
lucid <- lucid[which(start_date %in% good_dates), ]
names(table(unique(substr(lucid$StartDate, 1, 9))))
nrow(lucid) # 1,658

# who passed consent form?
length(which(lucid$IC == 4)) / nrow(lucid) # 97%

# who passed bot check?
length(which(is.na(lucid$bot_1) & is.na(lucid$bot_2) & is.na(lucid$bot_3) &
    is.na(lucid$bot_4) & lucid$bot_5 == 1 & lucid$bot_6 == 1)) / nrow(lucid) # 90%

# who passed Trump attention check?
length(which(lucid$trump == 3)) / nrow(lucid) # 85%

# subset to those who passed validation checks
lucid <- subset(lucid, lucid$trump == 3) 
nrow(lucid) # n = 1402

# remove respondents without a Lucid ID
lucid <- subset(lucid, !is.na(lucid$rid))
sum(is.na(lucid$rid))
nrow(lucid) # 1,402

# Checking for duplicate RIDs
length(unique(lucid$rid)) 
lucid <- subset(lucid, !duplicated(lucid$rid))
sum(duplicated(lucid$rid))
nrow(lucid) # 1,076 respondents remaining

# index variable to track R
lucid$index <- 1:nrow(lucid)

summary(lucid$total_time) # updated survey time


#### Coding Loss Aversion Parameter ####

# first switching task
task1 <- data.frame(lucid$la1_1, lucid$la1_2, lucid$la1_3, lucid$la1_4,
  lucid$la1_5, lucid$la1_6, lucid$la1_7, lucid$la1_8, lucid$la1_9, lucid$la1_10,
  lucid$la1_11, lucid$la1_12, lucid$la1_13, lucid$la1_14)

# second switching task
task2 <- data.frame(lucid$la2_1, lucid$la2_2, lucid$la2_3, lucid$la2_4,
  lucid$la2_5, lucid$la2_6, lucid$la2_7, lucid$la2_8, lucid$la2_9, lucid$la2_10,
  lucid$la2_11, lucid$la2_12, lucid$la2_13, lucid$la2_14)

# third switching task
task3 <- data.frame(lucid$la3_1, lucid$la3_2, lucid$la3_3, lucid$la3_4,
  lucid$la3_5, lucid$la3_6, lucid$la3_7)

# missing
length(which(is.na(task1[,1]))) # n = 10 skip first gamble
length(which(is.na(task2[,1]))) # n = 12
length(which(is.na(task3[,1]))) # n = 18

## obtain prospect theory parameter estimates

# Tables from Tanaka et al. (2010) supplemental materials (see also our Supplemental Materials)
# rows are switch in series 1, columns are series 2, 15 = Never switch
sigma_table <- matrix(c(
1.50, 1.40, 1.35, 1.25, 1.15, 1.10, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.65, 0.55, 0.50,
1.40, 1.30, 1.25, 1.15, 1.10, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.60, 0.55, 0.50,
1.30, 1.20, 1.15, 1.10, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.55, 0.50, 0.45,
1.20, 1.15, 1.05, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.50, 0.45, 0.40,
1.15, 1.05, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.40, 0.35,
1.05, 1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35,
1.00, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30,
0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25,
0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20,
0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20, 0.20,
0.80, 0.70, 0.65, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20, 0.15, 0.15,
0.75, 0.65, 0.60, 0.55, 0.50, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20, 0.20, 0.15, 0.10,
0.65, 0.60, 0.55, 0.50, 0.45, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20, 0.15, 0.15, 0.10, 0.10,
0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.35, 0.30, 0.25, 0.20, 0.15, 0.10, 0.10, 0.10, 0.05,
0.50, 0.45, 0.40, 0.40, 0.35, 0.30, 0.30, 0.25, 0.20, 0.15, 0.10, 0.10, 0.05, 0.05, 0.05),
  nrow = 15, byrow = T)

alpha_table <- matrix(c(
0.60, 0.75, 0.75, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20, 1.25, 1.30, 1.40, 1.45,
0.60, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20, 1.25, 1.35, 1.40,
0.55, 0.60, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20, 1.25, 1.30,
0.50, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20, 1.25,
0.45, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20,
0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10, 1.15,
0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05, 1.10,
0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.05,
0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00,
0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95,
0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90,
0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85,
0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80,
0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75,
0.05, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.45, 0.55, 0.55, 0.65, 0.60),
  nrow = 15, byrow = T)

# potential outcomes for third switching task for each option (A and B)
optionA_win <- c(25,4,1,1,1,1,1)
optionA_lose <- c(4,4,4,4,8,8,8) # cancel negatives for ease later
optionB_win <- rep(30,7)
optionB_lose <- c(21,21,21,16,16,14,11)

# function to compute quantities of interest
lambda_calculator <- function() {
  
  # initialize vectors to store quantities of interest
  sigma <- c()
  alpha <- c()
  lambda <- c()
  lambda_high <- lambda_low <- c()
  la_switch1 <- la_switch2 <- la_switch3 <- c()
  
  # determine switch points on 3 tasks
  for(i in 1:nrow(lucid)) {
    switch1 <- 14 - length(which(is.na(task1[i,]))) # gives switching point
    if(switch1 == 14)
      switch1 <- 16 - task1[i,14] # never assigned 15, last switch assigned 14
    la_switch1[i] <- switch1
    
    switch2 <- 14 - length(which(is.na(task2[i,])))
    if(switch2 == 14)
      switch2 <- 16 - task2[i,14]
    la_switch2[i] <- switch2
    
    switch3 <- 7 - length(which(is.na(task3[i,])))
    if(switch3 == 7)
      switch3 <- 9 - task3[i,7] # never assigned 8, last switch assigned 7
    la_switch3[i] <- switch3
    
    if(switch1 == 0 | switch2 == 0 | switch3 == 0) {
      # if R didnt complete task, cannot calculate params
      sigma[i] <- alpha[i] <- lambda[i] <- lambda_low[i] <- lambda_high[i] <- NA
      next
    }
    
    # assign sigma & alpha
    sigma[i] <- sigma_table[switch1, switch2]
    alpha[i] <- alpha_table[switch1, switch2]
    
    # calculate lambda inequality
    if(switch3 != 1) {
      lambda_low[i] <- (optionA_win[switch3-1]^sigma[i] - optionB_win[switch3-1]^sigma[i]) / 
        (optionA_lose[switch3-1]^sigma[i] - optionB_lose[switch3-1]^sigma[i])  
    } else {lambda_low[i] <- NA}
    
    if(switch3 != 8) {
      lambda_high[i] <- (optionA_win[switch3]^sigma[i] - optionB_win[switch3]^sigma[i]) / 
        (optionA_lose[switch3]^sigma[i] - optionB_lose[switch3]^sigma[i]) 
    } else {lambda_high[i] <- NA}
    
    ## return lambda midpoint
    if(is.na(lambda_low[i])) {
      lambda[i] <- lambda_high[i]
    } else if(is.na(lambda_high[i])) {
      lambda[i] <- lambda_low[i]
    } else { lambda[i] <- (lambda_low[i] + lambda_high[i]) / 2 }
  }
  
  vars <- list(la_switch1, la_switch2, la_switch3, sigma, alpha, lambda,
               lambda_low, lambda_high)
  return(vars)
}
loss_params <- lambda_calculator()

lucid$switch1 <- loss_params[[1]]
lucid$switch2 <- loss_params[[2]]
lucid$switch3 <- loss_params[[3]]
lucid$sigma <- loss_params[[4]]
lucid$alpha <- loss_params[[5]]
lucid$lambda <- loss_params[[6]]
lucid$lambda_low <- loss_params[[7]]
lucid$lambda_high <- loss_params[[8]]

# assign lambda values to negativity bias measure and standardize
lucid$neg_bias_std <- (lucid$lambda - mean(lucid$lambda, na.rm = T)) /
                        sd(lucid$lambda, na.rm = T)

## 1/3 to 3 robustness check measure
lucid$lambda_3 <- lucid$lambda
lucid$lambda_3 <- ifelse(lucid$lambda_3 < 1/3, NA, lucid$lambda_3)
lucid$lambda_3 <- ifelse(lucid$lambda_3 > 3, NA, lucid$lambda_3)
lucid$neg_bias_std_3 <- (lucid$lambda_3 - mean(lucid$lambda_3, na.rm = T)) /
  sd(lucid$lambda_3, na.rm = T)

#### Diagnostic Figures ####

# table with % of each switch point
switch1_table <- switch2_table <- switch3_table <- c()
for(i in 1:15) {
  switch1_table[i] <- length(which(loss_params[[1]] == i))
  switch2_table[i] <- length(which(loss_params[[2]] == i))
  switch3_table[i] <- length(which(loss_params[[3]] == i))
}
switch3_table <- switch3_table[1:8]
switch1_table <- round(switch1_table / sum(switch1_table),3)
switch2_table <- round(switch2_table / sum(switch2_table),3)
switch3_table <- round(switch3_table / sum(switch3_table),3)

# hist for each param w/median and mean text
hist(loss_params[[4]], xlab = "Sigma", main = "")
text(0.75,120, paste("Mean:",round(mean(loss_params[[4]], na.rm = T),2)))
text(0.75,115, paste("Median:",round(median(loss_params[[4]], na.rm = T),2)))
text(0.75,110, paste("SD:",round(sd(loss_params[[4]], na.rm = T),2)))

hist(loss_params[[5]], xlab = "Alpha", main = "")
text(0.75,250, paste("Mean:",round(mean(loss_params[[5]], na.rm = T),2)))
text(0.75,240, paste("Median:",round(median(loss_params[[5]], na.rm = T),2)))
text(0.75,230, paste("SD:",round(sd(loss_params[[5]], na.rm = T),2)))

hist(loss_params[[6]], xlab = "Lambda", main = "")
text(6,500, paste("Mean:",round(mean(loss_params[[6]], na.rm = T),2)))
text(6,480, paste("Median:",round(median(loss_params[[6]], na.rm = T),2)))
text(6,460, paste("SD:",round(sd(loss_params[[6]], na.rm = T),2)))

corrs <- as.data.frame(cbind(loss_params[[4]], loss_params[[5]], loss_params[[6]]))
cor(corrs[,1:3], use="complete.obs")

#### Coding Dependent Variables ####

# all coded such that higher values closer to conservatism expectation

#### Openness ####

## flip questions that were reverse coded
lucid$ipip1 <- 6 - lucid$ipip1
lucid$ipip5 <- 6 - lucid$ipip5

## average and normalize
open_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$ipip1[i], lucid$ipip2[i], lucid$ipip3[i], lucid$ipip4[i],
    lucid$ipip5[i])
  
  set <- na.omit(set)
  open_means <- append(open_means, mean(set))
}

lucid$open <- (open_means - 1) / 4

#### NFC ####

## average and normalize
nfc_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$nfc1[i], lucid$nfc2[i], lucid$nfc3[i], lucid$nfc4[i],
    lucid$nfc5[i], lucid$nfc6[i], lucid$nfc7[i], lucid$nfc8[i],
    lucid$nfc9[i], lucid$nfc10[i])
  
  set <- na.omit(set)
  nfc_means <- append(nfc_means, mean(set))
}

lucid$nfc <- (nfc_means - 1) / 5


#### Conservation ####

## fix error where schw2-5 6 coded as 8
lucid$schw2 <- ifelse(lucid$schw2 == 8, 6, lucid$schw2)
lucid$schw3 <- ifelse(lucid$schw3 == 8, 6, lucid$schw3)
lucid$schw4 <- ifelse(lucid$schw4 == 8, 6, lucid$schw4)
lucid$schw5 <- ifelse(lucid$schw5 == 8, 6, lucid$schw5)

##flip questions that were reverse coded
lucid$schw2 <- 7 - lucid$schw2
lucid$schw3 <- 7 - lucid$schw3
lucid$schw5 <- 7 - lucid$schw5

## average and normalize
conserve_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$schw1[i], lucid$schw2[i], lucid$schw3[i], lucid$schw4[i],
    lucid$schw5[i])
  
  set <- na.omit(set)
  conserve_means <- append(conserve_means, mean(set))
}

lucid$conserve <- (conserve_means - 1) / 5

#### Authoritarianism ####

## flip reverse coded items
lucid$auth3 <- 3 - lucid$auth3
lucid$auth5 <- 3 - lucid$auth5

## average and normalize
auth_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$auth1[i], lucid$auth2[i], lucid$auth3[i], lucid$auth4[i],
    lucid$auth5[i])
  
  set <- na.omit(set)
  auth_means <- append(auth_means, mean(set))
}

lucid$auth <- auth_means - 1

#### Moral Traditionalism ####

## flip reverse coded items
lucid$trad2 <- 8 - lucid$trad2
lucid$trad3 <- 8 - lucid$trad3

## average and normalize
trad_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$trad1[i], lucid$trad2[i], lucid$trad3[i], lucid$trad4[i])
  
  set <- na.omit(set)
  trad_means <- append(trad_means, mean(set))
}

lucid$trad <- (trad_means - 1) / 6

#### Limited Gov ####

## flip reverse coded items
lucid$lim2 <- 3 - lucid$lim2
lucid$lim3 <- 3 - lucid$lim3

## average and normalize
limgov_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$lim1[i], lucid$lim2[i], lucid$lim3[i])
  
  set <- na.omit(set)
  limgov_means <- append(limgov_means, mean(set))
}

lucid$limgov <- limgov_means - 1

#### Social Policy ####

## flip reverse coded items 
gm_recode <- ifelse(lucid$gay == 1, 1, ifelse(lucid$gay == 2, 3, 2))
lucid$gay <- gm_recode
lucid$abort <- 5 - lucid$abort

## place all on same scale
lucid$gay <- (lucid$gay - 1) / 2
lucid$affirm <- (lucid$affirm - 1) / 5
lucid$imm <- (lucid$imm - 1) / 3
lucid$abort <- (lucid$abort - 1) / 3

## average and normalize
social_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$gay[i], lucid$affirm[i], lucid$imm[i], lucid$abort[i])
  
  set <- na.omit(set)
  social_means <- append(social_means, mean(set))
}

lucid$social <- social_means

#### Economic Policy ####

## flip reverse coded items
lucid$ssec <- 7 - lucid$ssec

## place all on same scale
lucid$insure <- (lucid$insure - 1) / 6
lucid$ssec <- (lucid$ssec - 1) / 6
lucid$wage <- (lucid$wage - 1) / 5
lucid$tax <- (lucid$tax - 1) / 3

## average and normalize
economic_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$insure[i], lucid$ssec[i], lucid$wage[i], lucid$tax[i])
  
  set <- na.omit(set)
  economic_means <- append(economic_means, mean(set))
}

lucid$economic <- economic_means

#### Political Identity ####

## recode pid7
lucid$pid7 <- ifelse(!is.na(lucid$dstr), lucid$dstr, 
  ifelse(!is.na(lucid$rstr), lucid$rstr, lucid$lean))
table(lucid$pid7[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$pid7[!is.na(lucid$neg_bias_std)])) # 47

## ideo
table(lucid$ideo[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$ideo[!is.na(lucid$neg_bias_std)])) # 47

## average and normalize
pol_id_means <- c()

for(i in 1:nrow(lucid)) {
  set <- c(lucid$pid7[i], lucid$ideo[i])
  
  set <- na.omit(set)
  pol_id_means <- append(pol_id_means, mean(set))
}

lucid$pol_id <- (pol_id_means - 1) / 6

#### Code Remaining Individual Issues ####

# Military Spending
lucid$milit <- 7 - lucid$milit # flip
lucid$milit <- (lucid$milit - 1) / 5

# Imports
lucid$import <- (lucid$import - 1) / 5

#### Coding Controls ####

## overall sample size (# of respondents who have scores for negativity bias)
sum(!is.na(lucid$neg_bias_std)) # 1,056

## Age
lucid$age <- 2018 - lucid$yrborn
mean(lucid$age[!is.na(lucid$neg_bias_std)], na.rm = T) # 50
lucid$age <- (lucid$age - min(lucid$age, na.rm = T)) / 
  (max(lucid$age, na.rm = T) - min(lucid$age, na.rm = T))

## Gender
length(which(lucid$gender == 3)) # how many select other
lucid$female <- ifelse(lucid$gender == 2, 1, 0)
table(lucid$female[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$female[!is.na(lucid$neg_bias_std)])) # 51%

## Black
lucid$black <- ifelse(is.na(lucid$race_2) | lucid$race_2 == 0, 0, 1)
table(lucid$black[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$black[!is.na(lucid$neg_bias_std)])) # 13%

## Hispanic
lucid$hisp <- ifelse(is.na(lucid$race_3) | lucid$race_3 == 0, 0, 1)
table(lucid$hisp[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$hisp[!is.na(lucid$neg_bias_std)])) # 12%

## Education
table(lucid$educ[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$educ[!is.na(lucid$neg_bias_std)])) # 27%, 37%, 25%, 11% 
lucid$educ <- (lucid$educ - min(lucid$educ, na.rm = T)) / (max(lucid$educ, na.rm = T) - min(lucid$educ, na.rm = T))

## Income
table(lucid$income[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$income[!is.na(lucid$neg_bias_std)])) # 5%, 48%, 34%, 9%, 4% 
lucid$income <- (lucid$income - min(lucid$income, na.rm = T)) / (max(lucid$income, na.rm = T) - min(lucid$income, na.rm = T))

## Unemployed
lucid$unemp <- ifelse(lucid$employ == 3, 1, 0)
table(lucid$unemp[!is.na(lucid$neg_bias_std)]) / sum(table(lucid$unemp[!is.na(lucid$neg_bias_std)])) # 9% 

#### Coding Political Engagement ####

## flips, recodes, normalizing
lucid$attention <- (lucid$attention - 1) / 4

lucid$news <- lucid$news / 7

correct1 <- ifelse(lucid$kn1 == 1, 1, 0)
correct2 <- ifelse(lucid$kn2 == 3, 1, 0)
correct3 <- ifelse(lucid$kn3 == 3, 1, 0)
correct4 <- ifelse(lucid$kn4 == 2, 1, 0)
correct5 <- ifelse(lucid$kn5 == 4, 1, 0)
total_corr <- c()
for(i in 1:length(lucid$kn1)) {
  total_corr <- append(total_corr,
    sum(correct1[i],correct2[i],correct3[i],correct4[i],correct5[i]))
}
lucid$know <- total_corr / 5

## average
engage_means <- c()
for(i in 1:nrow(lucid)) {
  set <- c(lucid$attention[i], lucid$news[i], lucid$know[i])
  
  set <- na.omit(set)
  engage_means <- append(engage_means, mean(set))
}

lucid$engage <- engage_means

#### Export Final Dataset ####
write.csv(lucid, 'neg_bias_analysis_file_19.csv')

